var valHandle; //定时器
const app = getApp()
const mta = require('../../../../utils/mta_analysis.js'); //引入腾讯埋点文件
const quote = app.globalData.quote;
const tool = quote.tool
const api = quote.api;
const innerAudioContext = app.globalData.innerAudioContext; //引入音频播放器
Page({

    /**
     * 页面的初始数据
     */
    data: {
        time: 0,
        scale: app.globalData.scale,
        endTask: true,
        gameThreeAllData: null,
        isLastOne: false,
        over: false, //游戏结束
        isDoubbe: true,
        isBrang: app.globalData.isBrang,
        isShowGuidance: true,
        // animation: null,
        main: false,
        gameStatus: app.globalData.gameStatus,
        cpArr: ['', ''],
        ismaintwo: false,
        core: true,
        index: 0,
        stepText: 30, //设置倒计时初始值
        aaa: 0,
        isShowEndTip: false,
        GameThreeList: '',
        gameThreeData: '',
        answer: null,
        num: 0, //
        isClick: false,
        bac: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let that = this
        mta.Page.init(); //初始化腾讯埋点
        innerAudioContext.stop()
        that.setData({
            imgUrl: app.globalData.imgUrl,
            gameStatus: app.globalData.gameStatus,
        })
        // setTimeout(() => {
        //     that.clickStartBtn()
        //     that.setData({
        //         isShowGuidance: false,
        //         main: true,
        //     })
        // }, 2000)
        that.GameThreeList()
    },
    onReady: function() {

    },
    beginGame: function() {
        var that = this
        that.clickStartBtn()
        that.setData({
            isShowGuidance: false,
            main: true,
        })
    },
    //倒计时
    clickStartBtn: function() {
        var that = this
        //  that.data.stepText = 20 //重新设置一遍初始值，防止初始值被改变
        var step = that.data.stepText; //定义倒计时
        var scale = that.data.scale; //定义倒计时
        var num = -0.5;
        var decNum = 2 / step / 10
        clearInterval(valHandle)

        function drawArc(endAngle) {
            const ctx = wx.createCanvasContext("bgCanvas")
            ctx.setLineWidth(10 * scale)
            ctx.arc(50 * scale, 75 * scale, 25 * scale, 0, 5 * Math.PI)
            ctx.setStrokeStyle('white')

            ctx.stroke()

            ctx.beginPath()
            // ctx.setLineCap('round')
            ctx.setLineWidth(7 * scale)
            ctx.arc(50 * scale, 75 * scale, 25 * scale, 1.5 * Math.PI, endAngle, true)
            if (that.data.stepText == 0) {
                that.setData({
                    main: false,
                })
                if (that.data.num == that.data.GameThreeList.length) {
                    console.log('通关')
                    clearInterval(valHandle) //销毁定时器
                    that.setData({
                        main: true,
                    })
                } else if (that.data.num != that.data.GameThreeList.length) {
                    console.log('未通关')
                    clearInterval(valHandle) //销毁定时器
                    that.setData({
                        over: true,
                    })
                    tool.statistics(app.globalData.openId, app.globalData.moduleId, 2, 3, 2)

                }
            }
            ctx.setStrokeStyle('#FFD734')
            ctx.stroke()
            ctx.draw()
        }

        valHandle = setInterval(function() {
            that.setData({
                stepText: parseInt(step)
            })
            step = (step - 0.1).toFixed(1)
            num += decNum
            drawArc(num * Math.PI)
            if (that.data.isLastOne) {
                clearInterval(valHandle) //销毁定时器
            }
            if (step <= 0) {
                clearInterval(valHandle) //销毁定时器

            }

        }, 100)
    },

    GameThreeList() {
        let that = this
        var dataDic = {
            GameMark: 3,
            ModuleId: app.globalData.moduleId

        };
        api.methodBasicEnglish_GetGameData({
            dataDic,
            success: function(res) {
                console.log(res, '59')
                that.setData({
                    GameThreeList: res.data.data.GameList[0].OptionList,
                    gameThreeAllData: res.data.data.GameList,
                    answer: res.data.data.GameList[0].ChineseSentence,
                    gameThreeData: res.data.data.GameList[0]
                })
            }
        })
    },
    //选择答案
    clickOne(e) {
        let that = this
        if (that.data.isClick) {
            console.log('重读点击')
            return
        }
        that.setData({
            isClick: true
        })
        var cpArr = that.data.cpArr
        var num = that.data.num
        var statu = e.currentTarget.dataset.statu
        var text = e.currentTarget.dataset.text
        var _index = e.currentTarget.dataset.index
        cpArr.splice(_index, 1, 1)
        that.setData({
            cpArr: cpArr
        })
        if (statu == true) {
            console.log('正确')
            innerAudioContext.src = '/src/record/true1.mp3'
            innerAudioContext.play()
            that.data.gameThreeAllData.splice(0, 1)
            var num2 = num + 1
            if (that.data.gameThreeAllData.length <= 0) {
                console.log('最后一题过关')
                var time = 30 - that.data.stepText
                var courseName = app.globalData.courseName
                var timeStr = '0~10'
                if (time > 0 && time <= 10) {
                    timeStr = '0~10'
                } else if (time > 10 && time <= 15) {
                    timeStr = '11~15'
                } else if (time > 15 && time <= 25) {
                    timeStr = '16~25'
                } else if (time > 25 && time <= 30) {
                    timeStr = '26~30'
                } else {
                    timeStr = '30以上'
                }
                app.aldstat.sendEvent('通关时长', {
                    'duration': courseName + '-游戏3-通关时长-' + timeStr
                });
                mta.Event.stat("duration_statistics", {
                    'duration': courseName + '-游戏3-通关时长-' + timeStr
                });
                clearInterval(valHandle)
                if (that.data.gameStatus.GameOne == true || that.data.gameStatus.GameTwo == true) {
                    setTimeout(() => {
                        that.setData({
                            main: false,
                            ismaintwo: true,
                            isClick: false,
                            time: time
                        })
                    }, 1500)
                } else {
                    setTimeout(() => {
                        that.setData({
                            main: false,
                            bac: true,
                            isClick: false
                        })
                    }, 1500)
                }

            } else {
                setTimeout(() => {
                    cpArr.splice(_index, 1, 0)
                    that.setData({
                        cpArr: cpArr,
                    })
                    //    that.GameThreeList()
                    setTimeout(() => {
                        that.setData({
                            GameThreeList: that.data.gameThreeAllData[0].OptionList,
                            answer: that.data.gameThreeAllData[0].ChineseSentence,
                            gameThreeData: that.data.gameThreeAllData[0],
                            isClick: false
                        })
                    }, 200)

                }, 1500)

            }

        } else if (statu == false) {
            console.log('错误')
            innerAudioContext.src = '/src/record/false.mp3'
            innerAudioContext.play()

            that.data.gameThreeAllData.splice(0, 1)
            that.data.gameThreeAllData.push(that.data.gameThreeData)
            setTimeout(() => {
                cpArr.splice(_index, 1, 0)
                that.setData({
                    cpArr: cpArr,
                })
                setTimeout(() => {
                    that.setData({
                        GameThreeList: that.data.gameThreeAllData[0].OptionList,
                        answer: that.data.gameThreeAllData[0].ChineseSentence,
                        gameThreeData: that.data.gameThreeAllData[0],
                        isClick: false
                    })
                }, 200)
            }, 1500)
        }

    },
    //结束游戏
    endTask() {
        let that = this
        clearInterval(valHandle) //销毁定时器
        that.setData({
            isShowEndTip: true,
            core: false,
            stepText: that.data.stepText

        })
        console.log(that.data.stepText)
    },
    //确定
    confim(e) {
        let that = this
        tool.statistics(app.globalData.openId, app.globalData.moduleId, 2, 3, 1)
        wx.redirectTo({
            url: '/pages/indexPackage/courseList/courseList',
        })

    },
    //取消
    cancel() {
        let that = this
        that.setData({
            isShowEndTip: false,
            core: true,
            stepText: that.data.stepText
        })
        that.clickStartBtn(that.data.stepText)
        console.log(that.data.stepText)
    },
    //休息一下
    restTime() {
        let that = this
        that.unlockLevels(2)
    },
    //进入下一个任务模块
    gotoTask() {
        let that = this
        that.unlockLevels(1)
    },

    //确定
    rest() {
        if (app.globalData.addId == 0) {
            app.globalData.addId = 1

            wx.redirectTo({
                url: '../../../../pages/indexPackage/courseList/courseList?addId=' + 1,
            })
        } else {
            console.log('走缓存处理')
            wx.getStorage({
                key: 'removes',
                success: (res) => {
                    if (res.data) {
                        wx.redirectTo({
                            url: '../../../../pages/indexPackage/courseList/courseList?addId=' + 2,
                        })
                    }
                },

            })
        }
    },
    //重新挑战
    ChallengeAgain() {
        let that = this
        that.setData({
            over: false,
            main: true,
            isShowEndTip: false,
            isLastOne: false,
            stepText: 30
        })
        that.GameThreeList()
        that.clickStartBtn()
    },


    //解锁
    unlockLevels: function(type) {
        var dataDic = {
            SubtitleId: 3,
            ModuleId: app.globalData.moduleId,
            OpenId: app.globalData.openId
        };
        var that = this
        api.methodBasicEnglish_Unlock({
            dataDic: dataDic,
            success: function(res) {
                console.log('解锁成功', res)
                if (type == 1) {
                    var dataDic = {
                        ModuleId: app.globalData.moduleId,
                    }
                    api.methodBasicEnglish_GetTask({
                        dataDic,
                        success: function(res) {

                            app.globalData.taskStatus = res.data.data
                            console.log('数据请求成功', res)
                            if (res.data.data.TaskOne == true) {
                                wx.reLaunch({
                                    url: '/pages/taskPackage/pages/taskOne/taskOne',
                                })
                                return
                            } else if (res.data.data.TaskTwo == true) {
                                wx.reLaunch({
                                    url: '/pages/taskPackage/pages/taskTwo/taskTwo',
                                })
                                return
                            } else if (res.data.data.TaskThree == true) {
                                wx.reLaunch({
                                    url: '/pages/taskPackage/pages/taskThree/taskThree',
                                })
                                return
                            } else {
                                wx.reLaunch({
                                    url: '/pages/taskPackage/pages/taskFour/taskFour',
                                })
                            }
                        },
                        fail: function(err) {
                            console.log('数据请求失败', err)
                        }
                    })
                } else if (type == 2) {
                    if (app.globalData.addId == 0) {
                        app.globalData.addId = 1
                        wx.reLaunch({
                            url: '../../../../pages/indexPackage/courseList/courseList?addId=' + 1,
                        })
                    } else {
                        console.log('走缓存处理')
                        wx.getStorage({
                            key: 'removes',
                            success: (res) => {
                                if (res.data) {
                                    wx.reLaunch({
                                        url: '../../../../pages/indexPackage/courseList/courseList?addId=' + 2,
                                    })
                                }
                            },

                        })
                    }
                }

            },
            fail: function(err) {
                console.log('解锁失败', err)
            }
        })

    },
    //进入下一个游戏

    go_game: function() {
        var that=this
        if (that.data.gameStatus.GameOne == true) {
            wx.navigateTo({
                url: '/pages/gamePackage/pages/gamePicmate/gamePicmate',
            })
            return
        } else if (that.data.gameStatus.GameTwo == true) {
            wx.navigateTo({
                url: '/pages/gamePackage/pages/gameWordspell/gameWordspell',
            })
            return
        } else {
            that.unlockLevels(1)
        }
    },
    onShow: function() {
        let that = this
    },

    onHide: function() {

    },
    onUnload: function() {
        console.log('清理定时器')
        clearInterval(valHandle)
    },
    onShareAppMessage: function() {
        return {
            path: 'pages/indexPackage/index/index',
            success: function (res) {
                console.log('转发成功', res);
            },
            fail: function (err) {
                console.log('转发失败', err)
            }
        }
    }
})